package per.dg.multiprocesskotlin.aidl

import android.app.Service
import android.content.Intent
import android.os.IBinder

class AIDLService : Service() {

    lateinit private var mIServiceInterfaceStub : IServiceInterfaceStub
    private var mIClientInterface : IClientInterface? = null

    override fun onBind(intent: Intent): IBinder {
        return mIServiceInterfaceStub
    }

    override fun onCreate() {
        super.onCreate()
        mIServiceInterfaceStub = IServiceInterfaceStub()
    }

    inner class IServiceInterfaceStub : IServiceInterface.Stub(){
        override fun registerCallBack(mIClientInterface: IClientInterface?) {
            if(mIClientInterface != null){
                this@AIDLService.mIClientInterface = mIClientInterface
            }
        }

        override fun getUser(user : User): User? {
            if(this@AIDLService.mIClientInterface != null){
                this@AIDLService.mIClientInterface?.notice()
            }
            var user = user
            user?.name = "bin"
            return user
        }

        override fun unRegisterCallBack(mIClientInterface: IClientInterface?) {
            if(mIClientInterface != null){
                this@AIDLService.mIClientInterface = null
            }
        }

    }
}